package com.huizu.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huizu.api.domain.Coupon;
import com.huizu.api.domain.GoodType;
import com.huizu.api.service.IShopApiService;
import com.huizu.api.utils.PageUtil;

@Controller
@RequestMapping("/douMeiHui/shop/api")
public class ShopApiController {

	@Autowired
	private IShopApiService service;

	// 轮播图
	@PostMapping("/getRelation")
	@ResponseBody
	public Map<String, Object> getRelation(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			List<String> list = service.getRelation(param);
			rem.put("data", list);
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}

	// 一级商品类型
	@PostMapping("/getgoodType")
	@ResponseBody
	public Map<String, Object> getgoodType(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			List<GoodType> list = service.getgoodType(param);
			rem.put("data", list);
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}
	
	// 二级商品类型
	@PostMapping("/getTwogoodType")
	@ResponseBody
	public Map<String, Object> getTwogoodType(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			List<GoodType> list = service.getTwogoodType(param);
			rem.put("data", list);
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}

	// 推荐门店
	@PostMapping("/getShopList")
	@ResponseBody
	public Map<String, Object> getShopList(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			param = PageUtil.getPage(param);
			List<Map<String, Object>> list = service.getShopList(param);
			rem.put("data", list);
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}
    
	//门店详情
	@PostMapping("/getShopListDetail")
	@ResponseBody
	public Map<String, Object> getShopListDetail(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			rem.put("data", service.getShopDetail(param));
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}

	// 推荐商品
	@PostMapping("/getGoodsList")
	@ResponseBody
	public Map<String, Object> getGoodsList(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			param = PageUtil.getPage(param);
			List<Map<String, Object>> list = service.getGoodsByShopId(param);
			rem.put("data", list);
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}

	// 查询商品
	@PostMapping("/queryGoods")
	@ResponseBody
	public Map<String, Object> queryGoods(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> list = service.queryGoods(param);
			rem.put("data", list);
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}
	
	// 搜索排行及历史
	@PostMapping("/getSearchHis")
	@ResponseBody
	public Map<String, Object> getSearchHis(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			rem.put("data", service.getSearchHis(param));
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}

	//商品详情
	@PostMapping("/getGoodsDetail")
	@ResponseBody
	public Map<String, Object> getGoodsDetail(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			param = PageUtil.getPage(param);
			rem.put("data", service.getGoodsDetail(param));
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}

	// 根据类型查询商品
	@PostMapping("/getGoodsListByType")
	@ResponseBody
	public Map<String, Object> getGoodsListByType(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			param = PageUtil.getPage(param);
			List<Map<String, Object>> list = service.queryGoodsByCate(param);
			rem.put("data", list);
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}

	/**
	 * 获取商品规格
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/getSpec")
	@ResponseBody
	public Map<String, Object> getSpec(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			param = PageUtil.getPage(param);
			rem.put("data", service.getSpec(param));
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}

	/**
	 * 获取商品规格价格
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/getSpecPrice")
	@ResponseBody
	public Map<String, Object> getSpecPrice(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			rem.put("data", service.getSpecPrice(param));
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}

	/**
	 * 加入购物车
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/insertCart")
	@ResponseBody
	public Map<String, Object> insertCart(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			rem.put("data", service.insertCart(param));
			rem.put("code", 0);
			rem.put("msg", "加入成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "加入失败");
			e.printStackTrace();
		}
		return rem;
	}

	/**
	 * 购物车列表
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/getCartList")
	@ResponseBody
	public Map<String, Object> getCartList(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			rem.put("data", service.getCartList(param));
			rem.put("code", 0);
			rem.put("msg", "查询成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "查询失败");
			e.printStackTrace();
		}
		return rem;
	}
	
	/**
	 * 修改购物车数量
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/updateCartList")
	@ResponseBody
	public Map<String, Object> updateCartList(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			 service.updateCartNum(param);
			rem.put("code", 0);
			rem.put("msg", "编辑成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rem;
	}

	/**
	 * 删除
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/removeCart")
	@ResponseBody
	public Map<String, Object> removeCart(@RequestBody Map<String, Object> param) {
		Map<String, Object> rem = new HashMap<String, Object>();
		try {
			service.removeCart(param);
			rem.put("code", 0);
			rem.put("msg", "删除成功");
		} catch (Exception e) {
			rem.put("code", 1);
			rem.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rem;
	}

	/**
	 * 添加收货地址
	 */
	@PostMapping("/insertAddress")
	@ResponseBody
	public Map<String, Object> insertAddress(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			service.insertAddress(pm);
			map.put("code", 0);
			map.put("msg", "添加成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "添加失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 收货地址列表
	 */
	@PostMapping("/getAddressList")
	@ResponseBody
	public Map<String, Object> getAddressList(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("data", service.getAddressList(pm));
			map.put("code", 0);
			map.put("msg", "获取数据成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "获取数据失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 默认收货地址信息
	 */
	@PostMapping("/getAddress")
	@ResponseBody
	public Map<String, Object> getAddress(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("data", service.getAddress(pm));
			map.put("code", 0);
			map.put("msg", "获取数据成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "获取数据失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 删除收货地址信息
	 */
	@PostMapping("/deleteAddress")
	@ResponseBody
	public Map<String, Object> deleteAddress(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			service.deleteAddress(pm);
			map.put("code", 0);
			map.put("msg", "删除成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "删除失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 编辑收货地址信息
	 */
	@PostMapping("/updateDefaultFlag")
	@ResponseBody
	public Map<String, Object> updateDefaultFlag(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			service.updateDefaultFlag(pm);
			map.put("code", 0);
			map.put("msg", "编辑成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return map;
	}

	// 添加订单
	@RequestMapping("/insertOrder")
	@ResponseBody
	private Map<String, Object> insertOrder(@RequestBody Map<String, Object> map, HttpServletRequest request) {
		Map<String, Object> alipayMap = new HashMap<String, Object>();
		try {
			alipayMap = service.insertOrder(map, request);
		} catch (Exception e) {
			e.printStackTrace();
			alipayMap.put("code", 1);
			alipayMap.put("mag", "预下单失败");
		}
		return alipayMap;
	}

	/*
	 * 支付宝回调接口
	 */
	@RequestMapping(value = "/alipayCallbackUrl", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> alipayCallbackUrl(HttpServletRequest request) throws Exception {
		Map<String, Object> alipayMap = new HashMap<String, Object>();
		try {
			service.alipayCallbackUrl(request);
			alipayMap.put("code", "0");
			alipayMap.put("msg", "支付宝支付成功");
		} catch (Exception e) {
			e.printStackTrace();
			alipayMap.put("code", "1");
			alipayMap.put("msg", "支付宝支付失败");
		}
		return alipayMap;
	}

	/**
	 * 微信支付完成回调地址
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "WxcallbackUrl", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> WxcallbackUrl(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> wxMap = new HashMap<String, Object>();
		try {
			service.WxcallbackUrl(request, response);
			wxMap.put("code", 0);
			wxMap.put("msg", "微信支付成功");
		} catch (Exception se) {
			se.printStackTrace();
		}
		return wxMap;
	}

	/**
	 * 订单列表
	 */
	@PostMapping("/getOrderList")
	@ResponseBody
	public Map<String, Object> getOrderList(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			pm = PageUtil.getPage(pm);
			map.put("data", service.getOrderList(pm));
			map.put("code", 0);
			map.put("msg", "获取数据成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "获取数据失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 待付款订单支付接口
	 */
	@PostMapping("/payOrder")
	@ResponseBody
	public Map<String, Object> payOrder(@RequestBody Map<String, Object> pm, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("data", service.payOrder(pm, request));
			map.put("code", 0);
			map.put("msg", "预下单成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "预下单失败");
			e.printStackTrace();
		}
		return map;
	}

	/*
	 * 支付宝回调接口
	 */
	@RequestMapping(value = "/palipayCallbackUrl", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> palipayCallbackUrl(HttpServletRequest request) throws Exception {
		Map<String, Object> alipayMap = new HashMap<String, Object>();
		try {
			service.palipayCallbackUrl(request);
			alipayMap.put("code", "0");
			alipayMap.put("msg", "支付宝支付成功");
		} catch (Exception e) {
			e.printStackTrace();
			alipayMap.put("code", "1");
			alipayMap.put("msg", "支付宝支付失败");
		}
		return alipayMap;
	}

	/**
	 * 微信支付完成回调地址
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "pWxcallbackUrl", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> pWxcallbackUrl(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> wxMap = new HashMap<String, Object>();
		try {
			service.pWxcallbackUrl(request, response);
			wxMap.put("code", 0);
			wxMap.put("msg", "微信支付成功");
		} catch (Exception se) {
			se.printStackTrace();
		}
		return wxMap;
	}

	/**
	 * 确认收货
	 */
	@PostMapping("/updateOrderStaus")
	@ResponseBody
	public Map<String, Object> updateOrderStaus(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			service.updateOrderStatusBySub(pm);
			map.put("code", 0);
			map.put("msg", "编辑成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 未付款删除订单
	 */
	@PostMapping("/deleteOrder")
	@ResponseBody
	public Map<String, Object> deleteOrder(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			service.deleteOrder(pm);
			map.put("code", 0);
			map.put("msg", "删除成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "删除失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 申请退款
	 */
	@PostMapping("/returnOrder")
	@ResponseBody
	public Map<String, Object> returnOrder(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			service.returnOrder(pm);
			map.put("code", 0);
			map.put("msg", "申请成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "申请失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 订单退款
	 */
	@PostMapping("/returnPrice")
	@ResponseBody
	public Map<String, Object> returnPrice(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			map = service.returnPrice(pm);
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "退款失败");
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 领优惠卷-优惠卷列表
	 */
	@PostMapping("/getShopCoupon")
	@ResponseBody
	public Map<String, Object> getShopCoupon(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("data", service.getShopCoupon(pm));
			map.put("code", 0);
			map.put("msg", "查询成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "查询失败");
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 领取优惠卷
	 */
	@PostMapping("/insertMemberCoupon")
	@ResponseBody
	public Map<String, Object> insertMemberCoupon(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			map= service.insertMemberCoupon(pm);
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "领取失败");
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 可用优惠卷数量
	 */
	@PostMapping("/getMemberCouponNum")
	@ResponseBody
	public Map<String, Object> getMemberCouponNum(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			pm.put("shopIds", "1,"+pm.get("shopId"));
			pm.put("userFlag", 1);
			List<Coupon> list=service.getMemberCouponNum(pm);
			map.put("data", list.size());
			map.put("code", 0);
			map.put("msg", "查询成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "查询失败");
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 可用优惠卷列表
	 */
	@PostMapping("/getMemberCouponList")
	@ResponseBody
	public Map<String, Object> getMemberCouponList(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			pm.put("shopIds", "1,"+pm.get("shopId"));
			pm.put("userFlag", 1);
			List<Coupon> list=service.getMemberCouponNum(pm);
			map.put("data", list);
			map.put("code", 0);
			map.put("msg", "查询成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "查询失败");
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 我的优惠卷列表
	 */
	@PostMapping("/getUserMemberCouponList")
	@ResponseBody
	public Map<String, Object> getUserMemberCouponList(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			List<Coupon> list=service.getMemberCouponNum(pm);
			map.put("data", list);
			map.put("code", 0);
			map.put("msg", "查询成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "查询失败");
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 *  会员商品收藏
	 */
	@PostMapping("/updateCollect")
	@ResponseBody
	public Map<String, Object> updateCollect(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			service.updateCollect(pm);
			map.put("code", 0);
			map.put("msg", "编辑成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 *  会员商品浏览记录
	 */
	@PostMapping("/insertBrowsing")
	@ResponseBody
	public Map<String, Object> insertBrowsing(@RequestBody Map<String, Object> pm) {
		Map<String, Object> map = new HashMap<>();
		try {
			service.insertBrowsing(pm);
			map.put("code", 0);
			map.put("msg", "添加成功");
		} catch (Exception e) {
			map.put("code", 1);
			map.put("msg", "添加失败");
			e.printStackTrace();
		}
		return map;
	}
	
	
}
